﻿using System.Runtime.InteropServices;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using Windows.ApplicationModel;
using Windows.Storage.Pickers;
using System.Diagnostics;
using System.Threading.Tasks;

using Windows.Storage.Streams;
using Microsoft.Graphics.Canvas;
using Windows.UI;
using 纸片人.PageViews;
using System.Numerics;
using System.Reflection;
using 纸片人.InfoDataFolder;
using 纸片人.AttributeUsageFloder;
using Microsoft.International.Converters.PinYinConverter;
using System.Text.RegularExpressions;
using Windows.ApplicationModel.Core;
using Windows.UI.ViewManagement;

//using PictureSharde;

// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板

namespace 纸片人
{



    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private bool SplitViewOpenConvert(bool? value) => value == true;

        public MainPage()
        {
            this.InitializeComponent();
            Init();
            CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
            Window.Current.SetTitleBar(tileGrid);



        }

        private void Init()
        {
            ItemClickEventHandler ItemClick = (sender, e) =>
            {
                var pageInfo = (PageInfo)e.ClickedItem;
                myFrame.Navigate(pageInfo.Page, pageInfo);
            };

            gpuListView.ItemClick += ItemClick;
            cpuListView.ItemClick += ItemClick;
            cpuViewSource.Source = CreatePages(typeof(MainPage), "cpu").GroupBy(x => x.Initials).OrderBy(x => x.Key);
            var gpuCollection = CreatePages(typeof(MainPage), "gpu");
            gpuCollection = gpuCollection.OrderBy(x => x.Initials);
            gpuViewSource.Source = gpuCollection.GroupBy(x => x.Initials);

            pivotHeader.SelectionChanged += (s, e) =>
            {
                SplitButton.IsChecked = true;
            };
        }

        IEnumerable<PageInfo> CreatePages(Type assemblyType, string type)
        {
            //获取程序集
            Assembly assembly = assemblyType.Assembly;
            //获取程序集下的所有类定义
            Type[] typeInfos = assembly.GetTypes();
            foreach (TypeInfo typeInfo in typeInfos)
            {
                if (typeInfo.GetCustomAttribute<PageTypeAttribute>() != null)
                {
                    var attribute = typeInfo.GetCustomAttribute<PageTypeAttribute>();
                    if (attribute.PageType == type)
                    {
                        var lastModified = typeInfo.GetCustomAttributes<LastModifiedAttribute>();
                        var changes = new List<string>();
                        foreach (var change in lastModified)
                        {
                            var date = change.DateModified.ToString("yyyymmdd");
                            changes.Add(date + "  " + change.Changes);
                        }
                        var initials = GetPinYinInitials(attribute.PageName[0]);
                        var pageInfo = new PageInfo(attribute.PageName, "cpu", typeInfo, changes, initials);
                        yield return pageInfo;
                    }
                }
            }
        }

        public string GetPinYinInitials(Char ch)
        {
            if (string.IsNullOrEmpty(ch.ToString()))
                return "#";
            if (ChineseChar.IsValidChar(ch))
            {
                var cc = new ChineseChar(ch);
                return cc.Pinyins[0].Substring(0, 1).ToLower();
            }
            else
            {
                if (Regex.IsMatch(ch.ToString(), @"[a-z]", RegexOptions.IgnoreCase))
                    return ch.ToString();
                return "#";
            }
        }

        
    }

    public class Token
    {
        public int Key { get; set; }
        public int Value { get; set; }

        public int Position { get; set; }

        public int IsEnable { get; set; }
    }
}




